<form class="layui-form" action="" method="post" lay-filter="LAY-game-setting-form-edit">
    <script type="text/html" template lay-url="/admin/game_setting/{{d.params.id}}"
            lay-done="layui.data.done(d);">

        <input type="hidden" name="id" value="{{d.result.id}}"/>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">类型</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" value="{{ gameTypeMap[d.result.type] }}" readonly>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">正确得分</label>
                <div class="layui-input-inline">
                    <input type="text" name="correctScore" placeholder="请输入正确得分" autocomplete="off" class="layui-input"
                           lay-verify="required|number" value="{{ d.result.correctScore }}">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">错误得分</label>
                <div class="layui-input-inline">
                    <input type="text" name="errorScore" placeholder="请输入错误得分" autocomplete="off" class="layui-input"
                           lay-verify="required|number" value="{{ d.result.errorScore }}">
                </div>
            </div>
        </div>

        <div class="layui-form-item spell">
            <label class="layui-form-label">介绍语音</label>
            <div class="layui-upload">
                <button type="button" class="layui-btn layui-btn-normal" id="upload-audio">上传</button>
                <button type="button" class="layui-btn layui-btn-normal" id="audio-box">播放
                </button>
                <input type="hidden" name="introAudioUrl" value="{{ d.result.introAudioUrl }}">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="LAY-game-setting-form-update">立即提交</button>
            </div>
        </div>

    </script>
</form>

<script>
    layui.use(['admin', 'upload', 'form'], function () {
        var $ = layui.$
            , admin = layui.admin
            , setter = layui.setter
            , layer = layui.layer
            , response = setter.response
            , form = layui.form;

        layui.upload.render({
            elem: '#upload-audio',
            accept: 'audio'
            ,url: '/common/file/upload_audio'
            ,done: function(res){
                //如果上传失败
                if(res[response.statusName] !== response.statusCode.ok){
                    return layer.msg('上传失败');
                }
                //上传成功
                $('[name=introAudioUrl]').val(res.url);
                $("#audio-box").show();
            }
        });

        form.on('submit(LAY-game-setting-form-update)', function (data) {
            admin.req({
                url: '/admin/game_setting'
                , type: 'POST'
                , dataType: "json"
                , traditional: true
                , data: data.field
                , success: function (res) {
                    var statusCode = response.statusCode;
                    if (res[response.statusName] == statusCode.ok) {
                        layer.closeAll('page');  //关闭page的弹框
                        layer.msg('修改成功', {
                            icon: 1
                        });
                    }
                }
            });
            return false;
        });

    });

    layui.data.done = function (d) {
        layui.use(['form'], function () {
            var form = layui.form

            form.render(null, 'LAY-game-setting-form-edit'); //渲染该模板下的动态表单

            if (!$('[name=introAudioUrl]').val()) {
                $("#audio-box").hide();
            }

            $("#audio-box").on("click",  function() {
                player.play($('[name=introAudioUrl]').val())
            });
        });
    };
</script>
